package com.cxq.finalproject.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cxq.finalproject.R;
import com.cxq.finalproject.adapter.RecyclerAdapter;
import com.cxq.finalproject.entity.Item;
import com.cxq.finalproject.util.AdvanceDecoration;
import com.cxq.finalproject.util.GetImgIDByName;
import com.cxq.finalproject.util.MyClothHelper;
import com.cxq.finalproject.util.MyUserHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentCategoryMale#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentCategoryMale extends Fragment {
    private RecyclerView mRecyclerview;
    private List<Item> mData=new ArrayList<>();
    private RecyclerAdapter mAdapter;
    private MyClothHelper myClothHelper;
    private GetImgIDByName getImgIDByName;
    private SQLiteDatabase db;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public FragmentCategoryMale() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentStudyHistory.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentCategoryMale newInstance(String param1, String param2) {
        FragmentCategoryMale fragment = new FragmentCategoryMale();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_category_male, container, false);
        mRecyclerview = view.findViewById(R.id.fragment_male_recycler);
        GridLayoutManager manager=new GridLayoutManager(view.getContext(),2);
        //设置RecyclerView保持固定大小,这样可以优化RecyclerView的性能
        mRecyclerview.setHasFixedSize(true);
        mAdapter = new RecyclerAdapter(view.getContext(),getLayoutInflater(),mData);
        myClothHelper = new MyClothHelper(getActivity(), MyUserHelper.DB_NAME,null,1);
        initData();
        //设置布局
        mRecyclerview.setLayoutManager(manager);
        mRecyclerview.setAdapter(mAdapter);
        //添加分割线
        mRecyclerview.addItemDecoration(new AdvanceDecoration(view.getContext(), OrientationHelper.VERTICAL));
        return view;
    }

    public void initData(){
        db = myClothHelper.getWritableDatabase();
        Cursor c = db.query(myClothHelper.TABLE_NAME, null, "ClothClass='男装' and island='0'",null, null, null, "sale desc", null);
        while (c.moveToNext()){
            Log.e("第1列" , c.getString(0));
            getImgIDByName = new GetImgIDByName(c.getString(0));
            Item item =new Item(c.getString(6),getImgIDByName.getResId(),c.getString(1),c.getString(0));
            mData.add(item);
            Log.e("商品" , "Name：" + c.getString(0) + " ，价格：" + c.getString(1) + " 是否被租借："
                    + c.getString(2) + " ，借用者id：" + c.getString(3) + " ，简介：" + c.getString(4) + " ，销量：" +
                    c.getString(5) + " ，类型：" + c.getString(6));
        }
        db.close();
    }
}